<!-- user表单弹窗 -->
<form id="resume-form" lay-filter="resume-form" class="layui-form model-form">
    <input id="id" name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">上传人</label>
        <div class="layui-input-block">
            <input name="userId" placeholder="上传人不能更改" type="text" class="layui-input" maxlength="20"
                   />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">简历名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入简历名称" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-upload">
        <label class="layui-form-label">简历:</label>
        <div class="layui-upload layui-input-block">
            <input type="hidden" name="uri" required lay-verify="required" />
            <button type="button" class="layui-btn layui-btn-primary" id="fileBtn"><i class="layui-icon">&#xe67c;</i>选择文件</button>
        </div>
    </div>

    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="resume-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form','upload','config'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var upload=layui.upload;
        var config = layui.config;

        // 回显user数据
        var resume = admin.getTempData('t_resume');
        console.log(resume);
        if (resume) {
            form.val('resume-form', resume);
        }

        //文件上传
        upload.render({
            elem: '#fileBtn' ,
            url:  config.base_server + 'api-user/resume/upload',
            accept: 'file',
            auto: true ,
            before: function(obj){
                layer.load(); //上传loading
                this.data={access_token:config.getToken().access_token
                            ,userId:$("[name=userId]").val()
                            ,name:$("[name=name]").val()
                        };
            },
            done: function(data){
                layer.closeAll('loading');
                // console.log(data);
                $("[name=uri]").val(data.data);
            },
            error: function (data) {
                layer.closeAll('loading');
            }
        });

        // 表单提交事件
        form.on('submit(resume-form-submit)', function (data) {
            layer.load(2);
            if(data.field.id==null || data.field.id==""){
                admin.req('api-user/resume', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'PUT');
                return false;
            }else {
                admin.req('api-user/resume', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
                return false;
            }
            
        });
    });
</script>